﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TerrainProcessor;

namespace ScorchedTanks
{
    class SceneLobby : Scene
    {
        SpriteBatch spriteBatch;
        SpriteFont myFont;
        Networking globalNetworkObjet;
        ContentManager Content;
        GraphicsDeviceManager graphics;

        public SceneLobby(GraphicsDeviceManager g, ContentManager content, SpriteBatch sb, SpriteFont sf, Networking n)
        {
            myFont = sf;
            spriteBatch = sb;
            globalNetworkObjet = n;
            Content = content;
            graphics = g;
        }

        public override void update(float dTime)
        {
            globalNetworkObjet.networkSession.Update();
            base.update(dTime);
        }

        public override void draw(GraphicsDeviceManager g)
        {
            spriteBatch.Begin();
            float y = 100;

            spriteBatch.DrawString(myFont, "Lobby (A=ready, B=leave)",new Vector2(101, y + 1), Color.Black);
            spriteBatch.DrawString(myFont, "Lobby (A=ready, B=leave)",new Vector2(101, y), Color.White);

            y += myFont.LineSpacing * 2;

            foreach (NetworkGamer gamer in globalNetworkObjet.networkSession.AllGamers)
            {
                string text = gamer.Gamertag;
                 
                if (gamer.IsReady) text += " - ready!";

                spriteBatch.DrawString(myFont, text, new Vector2(170, y), Color.White);

                y += myFont.LineSpacing + 64;
            }
            spriteBatch.End();

            base.draw(g);
        }

        public override void handleInput(InputManager Input)
        {
            if (globalNetworkObjet.networkSession.IsEveryoneReady)
            {
                SceneManager.scenesToAdd.Add(new SceneBattle(graphics, Content, spriteBatch, myFont, globalNetworkObjet));
                readyToExit = true;
                globalNetworkObjet.currentGameState = GameState.Playing;
            }

            
            if (Input.isNewKeyPress(Keys.A))
            {
                foreach (LocalNetworkGamer gamer in globalNetworkObjet.networkSession.LocalGamers)
                    gamer.IsReady = true;
            }
            else if (Input.isNewKeyPress(Keys.B))
            {
                globalNetworkObjet.networkSession.Dispose();
                globalNetworkObjet.networkSession = null;
                SceneManager.scenesToAdd.Add(new SceneMenu(graphics, Content, spriteBatch, myFont, globalNetworkObjet));

                readyToExit = true;
            }
            

            base.handleInput(Input);
        }
    }
}
